package ro.myApp.security.service.user;

import javax.mail.Message;

import org.mule.api.MuleMessage;
import org.mule.transport.email.MailUtils;
import org.mule.transport.email.transformers.StringToEmailMessage;

import ro.myApp.security.service.Encryption;

public class ObjectArrayToReminderEmailMessageTransformer extends
		StringToEmailMessage {

	public ObjectArrayToReminderEmailMessageTransformer() {
		this.registerSourceType(Object[].class);
		this.setReturnClass(Message.class);
	}

	@Override
	protected void setContent(Object payload, Message email,
			String contentType, MuleMessage message) throws Exception {
		Object[] content = (Object[]) payload;

		String mailAddr = (String) content[0];
		email.setRecipients(Message.RecipientType.TO,
				MailUtils.stringToInternetAddresses(mailAddr));

		String username = (String) content[1];
		String code = Encryption.encrypt(content[2].toString());

		String contentStr = String
				.format("Dear %s,<br/>You have requested a password change. Please click the link below to complete this process.<br/>"
						+ "http://localhost:8080/myApp/changePassword.jspx?username=%s&code=%s",
						username, username, code);
		email.setContent(contentStr, contentType);
	}
}
